//
//  CZRefreshView.swift
//  Weibo
//
//  Created by cc on 10/26/17.
//  Copyright © 2017 cc. All rights reserved.
//

import UIKit



/// 刷新視圖 - 負責刷新相關的UI顯示和動畫
class CZRefreshView: UIView {
    
    //刷新狀態
    /*
         - IOS 系統中 UIView封裝的旋轉動畫
         - 默認順時針旋轉
         - 就近原則
         - 要想實現同方向旋轉， 需要調整一個 非常小的數字
         - 如果想實現360旋轉， 需要核心動畫 CABaseAnimation
     */
    var refreshState: CZRefreshState = .Normal {
        didSet {
            switch refreshState {
            case .Normal:
                
                //恢復狀態
                tipIcon?.isHidden = false
                indicator?.stopAnimating()
                
                tipLabel?.text = "繼續拉.."
                UIView.animate(withDuration: 0.15){
                    self.tipIcon?.transform = CGAffineTransform.identity
                }
                
            case .Pulling:
                tipLabel?.text = "放手刷新.."
                UIView.animate(withDuration: 0.15){
                    self.tipIcon?.transform = CGAffineTransform(rotationAngle: .pi - 0.001)
                }
                
            case .WillRefresh:
                tipLabel?.text = "正在刷新中.."
                //隱藏提示圖標
                tipIcon?.isHidden = true
                //顯示菊花
                indicator?.startAnimating()
            }
        }
    }
    
    //父視圖的高度 - 爲了刷新控件不需要關心當前具體的刷新視圖是誰
    var parentViewHeight: CGFloat = 0
    
    
    
    //提示圖標
    @IBOutlet weak var tipIcon: UIImageView!
    //提示標籤
    @IBOutlet weak var tipLabel: UILabel!
    //指示器
    @IBOutlet weak var indicator: UIActivityIndicatorView!
    
    
    class func refreshView() -> CZRefreshView {
        let nib = UINib(nibName: "CZMeituanRefreshView", bundle: nil)
        
        return nib.instantiate(withOwner: nil, options: nil)[0] as! CZRefreshView
        
    }
    
    
}
